<!-- <script type="text/javascript" src="/static/fmap_SDK_v2.7.0_Standalone/fengmap.min.js"></script> -->
<style> 
#efence_list{ position:absolute; left:0px; top:0px; background:#FFFFFF; width:400px;height:100% }
#efence_tool { position:absolute; left:420px; top:10px; background:#FFFFFF; width:100%;}
</style> 

<div id="map_box" style="width: 100%;height: 100%"></div>
  <div id="efence_list" class="layui-coutainer">
    <!-- 隐藏设备栏 -->
    <i style="position: absolute;right: 0" class="layui-icon layui-icon-close" onclick="dev_panle_sw('hide')"></i>
    <!-- 围栏列表开始 -->
    <form class="layui-form layui-form-pane" action="" style="width:auto;height: 15px;padding: 5px;">
      <div class="layui-form-item">
          <div class="layui-inline" >
              <div class="layui-input-inline" style="width: 280px">
                  <input type="text"  name="search_key"  placeholder="输入围栏名称,查找围栏详情" autocomplete="off" class="layui-input" style="height: 30px;">
              </div>
              <div class="layui-form-mid layui-word-aux"></div>
              <a type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="data-search-btn" >搜索</a>
          </div>
      </div>
    </form>
  <table id="efence_list_table" class="layui-table" lay-filter="efence_list_table"></table>
  <!-- 围栏列表结束 -->
  <!-- 单个设备绑定的围栏列表开始 -->
  <form class="layui-form layui-form-pane" action="" style="width:auto;height: 15px;padding: 5px;">
    <div class="layui-form-item">
        <div class="layui-inline" >
            <div class="layui-input-inline" style="width: 280px">
                <input type="text"  name="search_key"  placeholder="输入设备名称/ID,查找设备绑定围栏" autocomplete="off" class="layui-input" style="height: 30px">
            </div>
            <div class="layui-form-mid layui-word-aux"></div>
            <a type="button" class="layui-btn layui-btn-sm" lay-submit="" lay-filter="dev-efe-search-btn" >搜索</a>
        </div>
    </div>
  </form>
  <table id="efence_of_dev_table" class="layui-table" lay-filter="efence_of_dev_table"></table>
  <!-- 单个设备绑定的围栏列表结束 -->
</div>
<!-- 设备栏展示按钮 -->
<button id="show_sw" class="layui-btn" onclick="dev_panle_sw('show')" style="position: absolute;left:-20px;top: 40%;height: 150px;width: 10px;display: none;"><i class="layui-icon">&#xe66b;</i></button>
<script type="text/html" id="draw_efence">
    <a class="layui-btn layui-btn-sm" lay-event="draw_efence" style="display: none;">新建</a>
</script>

<div id="efence_tool" style="color:white;background-color:rgba(320,138,138,0)">
  <div class="layui-form-item">
    <!-- <div class="layui-inline" style="color:black">
      <label class="layui-form-label layui-bg-green">切换地图</label>
      <div class="layui-input-inline" style="width: 150px">
        <select id="map_filter_list" name="map_filter" placeholder="选择地图" class="layui-input" onchange="chose_map(this)" style="height: 30px">
          <option value="bd">百度地图</option>
          <option value="fmap">定制地图</option>
        </select>
      </div>
    </div> -->
    <div class="layui-inline" >
      <div class="layui-input-inline">
        <input type="text" list="addr_list"  id="search_address" onkeyup="search_map(this)"  placeholder="搜索地址" autocomplete="off" class="layui-input" style="height: 30px">
        <datalist id="addr_list"></datalist>
      </div>
      <div class="layui-form-mid layui-word-aux"></div>
      <!-- <a type="button" class="layui-btn layui-btn-sm" onclick="search_map($('#search_address').val())">搜索</a> -->
    </div>
    <div class="layui-inline" >
       <div class="layui-input-inline">
          <button class="layui-btn layui-btn-sm" id="reset_efence">重置围栏</button>
       </div>
    </div>
  </div>
</div>
<div id="draw_efence_tool" style="position: absolute;top:0px;right: 10px">
  <div class="layui-tab layui-tab-card"  lay-filter='chose_efence_kind'>
    <ul class="layui-tab-title">
      <li class="layui-this">圆形</li>
      <li>多边形</li>
    </ul>
    <div class="layui-tab-content">
      <div class="layui-tab-item layui-show">
        <form  class="layui-form layui-form-pane " id="circle_data_form" action=""  lay-filter="circle_data_form">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label layui-bg-green">圆心</label>
              <div class="layui-input-block">
                <input type="text" name="center" lay-verify="required" lay-reqtext="必填项!" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label layui-bg-green">半径</label>
              <div class="layui-input-block">
                <input type="text" name="radius" lay-verify="required" autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>
          <div id="circle_radius"></div>
          <br>
          <button class="layui-btn layui-btn-sm layui-btn-fluid" lay-submit="" lay-filter="circle_data_submit">绘制完成</button>
        </form>
      </div>
      <div class="layui-tab-item">
        <form  class="layui-form layui-form-pane " id="poly_data_form" action=""  lay-filter="poly_data_form">
          <div class="layui-form-item">
            <div class="layui-inline">
              <label class="layui-form-label layui-bg-green">当前坐标</label>
              <div class="layui-input-block">
                <input type="text" name="points" lay-verify="required"  autocomplete="off" class="layui-input">
              </div>
            </div>
          </div>
          <button class="layui-btn layui-btn-sm layui-btn-fluid" lay-submit="" lay-filter="circle_data_submit">绘制完成</button>
        </form>
      </div>
    </div>
  </div>
</div>
<!-- 围栏设置表格开始 -->

<form  class="layui-form layui-form-pane " id="handle_data_form" action=""  lay-filter="handle_data_form" style="display: none;padding: 10px">
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">围栏名称</label>
        <div class="layui-input-block">
          <input id="efe_name" type="text" name="name" lay-verify="required" lay-reqtext="必填项!" placeholder="不可重复" autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>
    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">生效时间</label>
        <div class="layui-input-inline">
          <input id="s_time"  type="text" name="s_time" lay-verify="required"  placeholder="开始时间" autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">结束时间</label>
        <div class="layui-input-inline">
          <input id="e_time"  type="text" name="e_time" lay-verify="required"  placeholder="结束时间" autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>

    <div class="layui-form-item">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">报警类型</label>
        <div class="layui-input-block">
          <input type="radio" name="alarm_kind" value="in" title="进围栏报警" checked="">
          <input type="radio" name="alarm_kind" value="out" title="出围栏报警">
          <input type="radio" name="alarm_kind" value="all" title="进出围栏报警">
        </div>
      </div>
    </div>
    <div class="layui-form-item" style="display: none;">
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">围栏类型</label>
        <div class="layui-input-inline">
          <input type="text" name="kind"  autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">所在地图</label>
        <div class="layui-input-inline">
          <input type="text" name="map_name"  autocomplete="off" class="layui-input">
        </div>
      </div>
      <div class="layui-inline">
        <label class="layui-form-label layui-bg-green">围栏内容</label>
        <div class="layui-input-inline">
          <input type="text" name="content" autocomplete="off" class="layui-input">
        </div>
      </div>
    </div>
  <div id="efence_bind_dev" class="layui-transfer" style="padding: 10px"></div>
</form>

<!-- 围栏设置表格结束 -->

<script type="text/html" id="currentTableBar">
    <a class="layui-btn layui-btn-xs data-count-edit" lay-event="edit">编辑</a>
    <!-- <a class="layui-btn layui-btn-xs layui-btn-danger data-count-delete" lay-event="delete">启/停</a> -->
</script>

<script type="text/javascript">


$('.layui-body').attr('style','padding:0px;bottom:0px;left:0px') 

var dev_panle_sw=function(command){
  if(command=='hide'){
    $('#efence_list').hide()
    $('#show_sw').show()
  }else{
    $('#efence_list').show()
    $('#show_sw').hide()
  } 
}
// 填充地图选择下拉框
// var map_obj_list={}
// for(var i=0;i<all_map_data.length;i++){
//   var map_obj=all_map_data[i]
//   var map_name=map_obj.name
//   map_obj_list[map_name]=map_obj
//   var opt='<option value="'+map_name+'">'+map_name+'</option>'
//   $('#map_filter_list').append(opt)
// }
// 监听地图切换

var chose_map=function(obj){
  //console.log('ok',obj.value,obj)
  var page_name='efence'
  if(obj.value!=='bd'){
    map_to_show=map_obj_list[obj.value]
    layui.data('map_to_show',{key:'map_to_show',value:JSON.stringify(map_to_show)})
    page_name=map_to_show.content.split('.')[1]+'_efence'
  }
  window.history.pushState(null, null, "/index/#"+page_name)
  turn_to_page(page_name)
}
var ajax_url='efence';
// 获取围栏数据，加载到围栏表格
var efence_info;

var efence_table=function(data){
table.render({
  elem: '#efence_list_table',
  data: data,
  toolbar:'#draw_efence',
  id:'efence_list_table',
  skin:'nob',
  size:'sm',
  height: (get_height-60)/2-20,
  cols: [[
      {type: "checkbox", fixed: "left"},
      {field: 'name', title: '名称',sort:true},
      {field: 'kind', title: '类型',sort:true,
      templet: function(d){
            var efe_kind={'circle':'圆形围栏','polygon':'多边形围栏','rectangle':'矩形围栏'}
            return efe_kind[d.kind]
          }
        },
        {field: 'device_id', title: '绑定设备',sort:true},
        {title: '操作', minWidth: 110, templet: '#currentTableBar', fixed: "right", align: "center"}

        ]],
  // page: true,
  limit:100
});

}

// 加载围栏数据
$.ajax({
    type: 'GET',
    url:"/efence/?what=get_efence&map_kind=fmap",
    async:true,
    dataType: 'json',
    success: function(data) {
      console.log('success',data)
      var fmap_efe=[]
      for(var i=0;i<data.length;i++){
        if(data[i].map_name==current_map.name){
          fmap_efe.push(data[i])
        }
      }
      efence_info=fmap_efe
      efence_table(efence_info)

    },
    error:function(e){
      console.log(e)
    }
});

// 新建围栏按钮监听
table.on('toolbar(efence_list_table)',function(obj){
  map.clearOverlays()
  use_draw_tool()

})
// 重置围栏
$('#reset_efence').click(function(){
  reset_efence()
})

var reset_efence=function(){
  if(efence_layer){
    efence_layer.removeAll()
  }
  document.getElementById("circle_data_form").reset();
  // document.getElementById("rect_data_form").reset();
  document.getElementById("poly_data_form").reset();
  polygon_pts=[]
}

//新建围栏功能，通过地图上画完围栏之后的回调函数实现该功能
var overlaycomplete = function(obj){
  document.getElementById("handle_data_form").reset();
  transfer.reload('binded_dev', {
                  data: device
                  ,value:[]
                  ,title: ['未绑定', '绑定']
                  ,height:320
                  ,width:280
                  ,showSearch: true
                  ,parseData: function(res){
                    return {
                      "value": res.device_id //数据值
                      ,"title": '名称：'+res.user_name+'    ID:'+ res.device_id //数据标题
                      ,"disabled": ''  //是否禁用
                      ,"checked": '' //是否选中
                    }
                  }
  })
  form.render()


  layer.confirm("是否绘制完毕？",
                {btn: ['是', '重绘']}, 
                function (index) {
                                  layer.closeAll('dialog');
                                  $('#efe_name').removeAttr('readonly')
                                  layer.open({
                                    type: 1
                                    ,title:'电子围栏设置'
                                    ,area:['700px','650px']
                                    ,offset:'60px'  
                                    ,closeBtn:0
                                    ,id: 'efe_data_form' //防止重复弹出
                                    ,content:$('#handle_data_form')
                                    ,btn: ['提交','关闭']
                                    ,shade: 0 //不显示遮罩
                                    ,btn1: function(){
                                      var base_info=form.val('handle_data_form')
                                      if(base_info['name']==''){
                                        layer.msg('请输入围栏名称')
                                        return false
                                      }
                                      var binded_dev=transfer.getData('binded_dev');
                                      base_info['device_id']=[]
                                      for(var i=0;i<binded_dev.length;i++){
                                        base_info['device_id'].push(binded_dev[i]['value'])
                                      }
                                      var efencedata=[]
                                      var efekindlist={'circle':'circle_data_form','rectangle':'rect_data_form','polygon':'poly_data_form'}
                                      var efe_val=form.val(efekindlist[draw_efence_kind])
                                      if(draw_efence_kind=='circle'){
                                        var rad=efe_val.radius
                                        var cent=efe_val.center.split(',')
                                        cent={'lng':cent[0],'lat':cent[1]}
                                        efencedata.push(rad)
                                        efencedata.push(cent)
                                      }else if(draw_efence_kind=='polygon'){
                                        for(var i=0;i<polygon_pts.length;i++){
                                          efencedata.push({'lng':polygon_pts[i].x,'lat':polygon_pts[i].y})
                                        }
                                      }
                                      base_info['content']=efencedata
                                      base_info['kind']=draw_efence_kind
                                      base_info['map_name']=current_map.name
                                      console.log(base_info)
                                      post_info_ajax(base_info,ajax_url,'add&map_kind=fmap',efence_table)
                                    }
                                    ,btn2: function(){
                                      layer.closeAll();
                                      reset_efence()

                                    }
                                  });

                                }, function(){
                                  reset_efence()
                                });

};

// 电子围栏点击事件
var efence_click=function(efe_name){
  var efe_detail;
  document.getElementById("handle_data_form").reset();

  for(var i=0;i<efence_info.length;i++){
    if(efence_info[i]['name']==efe_name){
      efe_detail=efence_info[i]
    }
  }

  form.val('handle_data_form',efe_detail)

  transfer.reload('binded_dev', {
                  data: device
                  ,value:eval(efe_detail['device_id'])
                  ,title: ['未绑定', '绑定']
                  ,height:320
                  ,width:280
                  ,showSearch: true
                  ,parseData: function(res){
                    return {
                      "value": res.device_id //数据值
                      ,"title": '名称：'+res.user_name+'    ID:'+ res.device_id //数据标题
                      ,"disabled": ''  //是否禁用
                      ,"checked": '' //是否选中
                    }
                  }
  })

  layer.open({
              type: 1
              ,title:'电子围栏详情'
              ,area:['700px','650px']
              ,offset:'60px'  
              ,id: 'efe_data_form' //防止重复弹出
              ,content:$('#handle_data_form')
              ,shade: 0 //不显示遮罩
            });

}



// 监听编辑按钮
table.on('tool(efence_list_table)',function(obj){
  $('#efe_name').attr('readonly','readonly')
  //console.log(obj)
  var efe_detail=obj.data;
  // 给表格赋值
  document.getElementById("handle_data_form").reset();
  form.val('handle_data_form',efe_detail)

  var org_bin_dev=eval(efe_detail['device_id'])
  transfer.reload('binded_dev', {
                  data: device
                  ,value:org_bin_dev
                  ,title: ['未绑定', '绑定']
                  ,height:320
                  ,width:280
                  ,showSearch: true
                  ,parseData: function(res){
                    return {
                      "value": res.device_id //数据值
                      ,"title": '名称：'+res.user_name+'    ID:'+ res.device_id //数据标题
                      ,"disabled": ''  //是否禁用
                      ,"checked": '' //是否选中
                    }
                  }
  })
  layer.open({
              type: 1
              ,title:'电子围栏设置'
              ,area:['700px','650px']
              ,offset:'60px'  
              ,closeBtn:0
              ,id: 'efe_data_form' //防止重复弹出
              ,content:$('#handle_data_form')
              ,btn: ['提交','关闭']
              ,shade: 0 //不显示遮罩
              ,btn1: function(){
                var base_info=form.val('handle_data_form')

                var binded_dev=transfer.getData('binded_dev');
                base_info['device_id']=[]
                for(var i=0;i<binded_dev.length;i++){
                  base_info['device_id'].push(binded_dev[i]['value'])
                }

                // 穿梭框进行绑定围栏与设备的操作，需要比较围栏建立之初的原始绑定设备列表
                // 历遍原始设备绑定列表，每个dev与变动后的dev列表比较，不在变动后的列表的设备，则加入del列表，否则为stay
                // 历遍变动后的dev列表，每个dev与原始设备绑定列表比较，不在原始列表的设备，则计入add列表
                var new_binded=base_info['device_id']
                var add_dev=[];
                var del_dev=[];
                var stay_dev=[]

                //计算从围栏解绑的设备
                for(var i=0;i<org_bin_dev.length;i++){
                  if(new_binded.indexOf(org_bin_dev[i])==-1){
                    del_dev.push(org_bin_dev[i])
                  }else{
                    stay_dev.push(org_bin_dev[i])
                  }
                }
                //计算新增绑定围栏的设备
                for(var i=0;i<new_binded.length;i++){
                  if(org_bin_dev.indexOf(new_binded[i])==-1){
                    add_dev.push(new_binded[i])
                  }
                }

                base_info['content']=eval(efe_detail.content)
                base_info['add_dev']=add_dev;
                base_info['del_dev']=del_dev;
                base_info['stay_dev']=stay_dev;
                
                console.log(base_info)
                // post_info_ajax(base_info,ajax_url,'edit&map_kind=fmap',efence_table)
              }
              ,btn2: function(){
                reset_efence()
              }
            });
})

// 点击围栏列表，显示围栏内容
var checkDiv=new Array();
// 监听设备列表点击
table.on('row(efence_list_table)', function(obj){
 //选中行同步选中复选按钮
    var tr = obj.tr;
    var div = tr.find("div.layui-unselect");
    //checkDiv为定义的属性用来缓存当前点击的行是否选中状态，未选择的选中，选中的取消选中，并进行相应的地图打点处理
    // console.log(obj.data.device_id)
    if(!checkDiv[obj.data.name]) {
      checkDiv[obj.data.name] = obj;
      div.addClass("layui-form-checked");
      efence_show(obj.data)

    } else {
      div.removeClass("layui-form-checked");
      delete checkDiv[obj.data.name];
      efence_remove(obj.data.name)

    }

});

// 地图上展示围栏
var efence_show_obj={}
var efence_show=function(row){
  var efencepath=row.content;
  efencepath=eval(efencepath);
  var efence_done
  if(row.kind=='circle'){
    efence_done=createCircleMaker(efencepath[1].lng,efencepath[1].lat,efencepath[0])
  }else if(row.kind=='polygon' | row.kind=='rectangle'){
    for(var i=0;i<efencepath.length;i++){
      efencepath[i].z=1
      efencepath[i].x=efencepath[i].lng
      efencepath[i].y=efencepath[i].lat
    }
    //console.log(efencepath)
    efence_done=createPolygonMaker(efencepath)
  }
  efence_done.name=row.name
  efence_show_obj[row.name]=efence_done
  fn_map.moveTo({x:efencepath[1].lng,y:efencepath[1].lat})
}

//删除地图上面显示的指定围栏
var efence_remove=function(efence_name){
  efence_layer.removeMarker(efence_show_obj[efence_name])
};



// ----打开fmap地图start---- //
var fn_map
var open_fmap=function(div_id,file){
  fn_map = new fengmap.FMMap({
    //渲染dom
    container: document.getElementById(div_id),
    //地图数据位置
    mapServerURL: '/static/map_data/',
    defaultMapScaleLevel: 19,
    //开发者申请应用下web服务的key
    key: '9ba50831cbdde4f07c79740dae2959ff',
    //开发者申请应用名称
    appName: 'map测试',
   });
  fn_map.openMapById(file);
}

// var map_detail;
// if(map_data.length!=0){
//   map_detail=map_data[0]
// }else{
//   layer.msg('无地图')
// }
var content=current_map.content
var overlay=eval('('+current_map.overlay+')')
var f_name=content.split('.')[0]
var l_name=content.split('.')[1]
// console.log(f_name)
open_fmap('map_box',f_name)
// form.val('trail_query_form',{'map_filter':map_to_show.name})
// $('#map_filter_list').val(map_to_show.name)

// ----打开fmap地图end---- //

//监听绘制类型选择
var draw_efence_kind='circle'
element.on('tab(chose_efence_kind)', function(data){
  var kind_obj={0:'circle',1:'polygon',2:'rectangle'}
  // 切换绘制类型，把之前的数据情况
  reset_efence()
  draw_efence_kind=kind_obj[data.index]

});

// 初始化围栏标记和标记层变量
var group
var efence_layer
// var efence_marker;
var polygon_pts=[]
// 监听地图点击事件，点击地图，开始围栏绘制
fn_map.on('loadComplete', function () {
  group = fn_map.getFMGroup(fn_map.focusGroupID);
  efence_layer = group.getOrCreateLayer('polygonMarker')
  searchAnalyser = new fengmap.FMSearchAnalyser(fn_map)
  fn_map.on('mapClickNode', function(event) {
    var target_obj=event.target
    // console.log(event.target.nodeType,fengmap.FMNodeType.IMAGE_MARKER,event.target.overlay_name)
    var x = event.eventInfo.coord.x
    var y = event.eventInfo.coord.y
    var z = event.eventInfo.coord.z
    var groupID= target_obj.groupID
    screean_loca=[x,y]
    var buttonType = event.domEvent.button;
    //console.log(x,y,z,groupID,buttonType)
    fn_map.moveTo({x:x,y:y+30,z:z,groupID:groupID,time:0.3,callback:function(){console.log(x,y,z,groupID)}})
    // 清除之前的围栏
    if(buttonType==0){
      efence_layer.removeAll()
      if(draw_efence_kind=='circle'){
        createCircleMaker(x,y,30)
      }else if(draw_efence_kind=='rectangle'){
        createRectangleMaker(x,y,30,30)
      }else if(draw_efence_kind=='polygon'){
        polygon_pts.push({'x':x,'y':y,'z':1})
        createPolygonMaker(polygon_pts)
      }
    }
    

    

    if( target_obj.name!=null && buttonType==2){
      //console.log(target_obj,target_obj.name)
      efence_click(target_obj.name)
    }else if( target_obj.overlay_name!=null && target_obj.show!=false){
      static_marker_inforwindow(target_obj)
    }else{

    }
  })
})

// 围栏绘制完毕，进行下一步围栏信息录入，设备绑定
form.on('submit(circle_data_submit)',function(obj){
  overlaycomplete(obj.field)
  return false
})

//穿梭框渲染
var efence_bind_dev=transfer.render({
  elem: '#efence_bind_dev'
  ,id:'binded_dev'
  ,data: device
  ,title: ['未绑定', '绑定']
  ,height:320
  ,showSearch: true
  ,parseData: function(res){
    return {
      "value": res.device_id //数据值
      ,"title": '名称：'+res.user_name+'ID:'+ res.device_id//数据标题
      ,"disabled": ''  //是否禁用
      ,"checked": '' //是否选中
    }
  }
})

// 时间框渲染
laydate.render({
  elem: '#s_time'
  ,type: 'datetime'
  // ,value:new Date(new Date(new Date().toLocaleDateString()).getTime())
  ,btns: ['clear','now', 'confirm']
});

laydate.render({
  elem: '#e_time'
  ,type: 'datetime'
  // ,value:new Date()
  ,btns: ['clear','now', 'confirm']
});


// 监听围栏搜索操作
form.on('submit(data-search-btn)', function (data) {

  var search_key = data.field['search_key'];
  if(search_key!==''){
    // 调用index页面搜索函数
    var search_result=search_func(efence_info,search_key,'name','kind')
    // 表格重载
    efence_table(search_result)
  }else{
    efence_table(efence_info)
  }
  return false;
});

// 监听指定设备的围栏搜索操作
form.on('submit(dev-efe-search-btn)', function (data) {

  var search_key = data.field['search_key'];
  if(search_key!==''){
    // 调用index页面搜索函数
    var search_result=search_func(device,search_key,'device_id','user_name')
    // 表格重载
    //console.log(search_result)
    if(search_result[0]['efence']=='0'){
      efence_of_dev_table([])
    }else{
      search_result=eval('('+search_result[0]['efence']+')')
      var show_data=[];
      for(var k in search_result){
        show_data.push(search_result[k])
      }
      efence_of_dev_table(show_data)
    }

    }else{
      efence_of_dev_table([])
    }
    return false;
});

// 根据设备id，查找设备绑定的围栏详情
var efence_of_dev_table=function(data){
table.render({
  elem: '#efence_of_dev_table',
  data: data,
  // toolbar:'#draw_efence',
  id:'efence_of_dev_table',
  skin:'nob',
  size:'sm',
  height: (get_height-60)/3,
  cols: [[
      {type: "checkbox", fixed: "left"},
      {field: 'name', title: '名称',sort:true},
      {field: 'kind', title: '类型',sort:true,
        templet: function(d){
            //console.log(d)
            var efe_kind={'circle':'圆形围栏','polygon':'多边形围栏','rectangle':'矩形围栏'}
            return efe_kind[d.kind]
          }
        },
        {field: 'alarm_kind', title: '报警类型',sort:true,
          templet: function(d){
            var alarm_kind_dict={'in':'进围栏报警','out':'出围栏报警','all':'进出围栏报警'}
            return alarm_kind_dict[d.alarm_kind]
          }
        },
        {field: 'time', title: '生效时间',sort:true,
          templet: function(d){
            var effect_time=d.s_time+'--'+d.e_time
            return effect_time
          }
        }
        // {title: '操作', minWidth: 110, templet: '#currentTableBar', fixed: "right", align: "center"}

        ]],
  // page: true,
  limit:100
});

}
efence_of_dev_table([])

// 点击围栏列表，显示围栏内容
var checkefe=new Array();
// 监听设备列表点击
table.on('row(efence_of_dev_table)', function(obj){
 //选中行同步选中复选按钮
    var tr = obj.tr;
    var div = tr.find("div.layui-unselect");
    //checkDiv为定义的属性用来缓存当前点击的行是否选中状态，未选择的选中，选中的取消选中，并进行相应的地图打点处理
    // console.log(obj.data.device_id)
    if(!checkefe[obj.data.name]) {
      checkefe[obj.data.name] = obj;
      div.addClass("layui-form-checked");
      efence_show(obj.data)

    } else {
      div.removeClass("layui-form-checked");
      delete checkefe[obj.data.name];
      efence_remove(obj.data.name)

    }

});

// 地图地址搜索功能
var searchAnalyser;
var select_obj;
var search_map=function(self){
  if(select_obj!=null){
    select_obj.selected=false
  }
  addr=self.value.trim();
  //console.log(addr)
  if(addr==''){
    layer.msg('请输入地址')
    return
  }

  var searchRequest = new fengmap.FMSearchRequest();
  searchRequest.keyword = addr;
  searchRequest.nodeType = fengmap.FMNodeType.MODEL;
  searchRequest.groupID =1

  var sortRes = searchAnalyser.getQueryResult(searchRequest, ['SINGLE']);

  $('#addr_list').empty()
  for(var i=0;i<sortRes.length;i++){
    if(addr!=sortRes[i].name){
      var opt='<option>'+sortRes[i].name+'</option>'
      $('#addr_list').append(opt)
    }else{
      //console.log(sortRes[i],sortRes[i].target.selected)
      sortRes[i].target.selected=true
      select_obj=sortRes[i].target
      fn_map.moveTo(sortRes[i].mapCoord)
    }
  }
  // $('#search_address').click()
  return sortRes;
}

//创建矩形标注
var createRectangleMaker=function (x,y,h,w) {
  efence_layer.removeAll()
  var efence_marker = new fengmap.FMPolygonMarker({
      //设置颜色
      color: '#9F35FF',
      //设置透明度
      alpha: 0.3,
      //设置边框线的宽度
      lineWidth: 3,
      //设置高度
      height: 5,
      //多边形的坐标点集数组
      points: {
          //设置为矩形
          type: 'rectangle',
          //设置此形状的中心坐标
          // center: {
          //     x: x,
          //     y: y
          // },
          //矩形的起始点设置，代表矩形的左上角。优先级大于center。
          startPoint: {
           x: x,
           y: y
           },
          //设置矩形的宽度
          width: w,
          //设置矩形的高度
          height: h
      }
  });
  efence_layer.addMarker(efence_marker)
  //console.log(efence_marker)
  form.val('rect_data_form',{'center':x.toString()+','+y.toString(),'width':w,'height':h})
  return efence_marker
}


function createCircleMaker(x,y,radius) {
  efence_layer.removeAll()
  var efence_marker = new fengmap.FMPolygonMarker({
      //设置颜色
      // color: '#3CF9DF',
      //设置透明度
      alpha: .3,
      //设置边框线的宽度
      lineWidth: 3,
      //设置高度
      height: 6,
      //多边形的坐标点集数组
      points: {
          //设置为圆形
          type: 'circle',
          //设置此形状的中心坐标
          center: {
              x: x,
              y: y
          },
          //设置半径
          radius: radius,
          //设置段数，默认为40段
          segments: 40
      }
  });
  efence_layer.addMarker(efence_marker);
  form.val('circle_data_form',{'center':x.toString()+','+y.toString(),'radius':radius})
  return efence_marker
}

/**
 * 创建自定义形状标注
 * coords 多边形的坐标点集数组
 * */
function createPolygonMaker(coords) {
  //实例化polygonMarker
  var efence_marker = new fengmap.FMPolygonMarker({
      //设置透明度
      alpha: 0.5,
      //设置边框线的宽度
      lineWidth: 3,
      //设置高度
      height: 7,
      //多边形的坐标点集数组
      points: coords
  });
  form.val('poly_data_form',{'points':coords[coords.length-1].x+','+coords[coords.length-1].y})
  efence_layer.addMarker(efence_marker)
  return efence_marker
}


//圆心围栏滑块渲染，开启输入框
slider.render({
  elem: '#circle_radius'
  ,value: 30
  ,max:200
  ,change: function(value){
    form.val('circle_data_form',{'radius':value})
    var circle_data=form.val('circle_data_form')
    if(circle_data.center!=''){
      var xy=circle_data.center.split(',')
      createCircleMaker(parseInt(xy[0]),parseInt(xy[1]),value)
    }
  }
});

//矩形滑块渲染，开启输入框
slider.render({
  elem: '#rectangle_radius'
  ,value: 30
  ,max:200
  ,range:true
  ,change: function(value){
    form.val('rect_data_form',{'width':value[1],'height':value[0]})
    var circle_data=form.val('rect_data_form')
    if(circle_data.center!=''){
      var xy=circle_data.center.split(',')
      createRectangleMaker(parseInt(xy[0]),parseInt(xy[1]),value[0],value[1])
    }
  }
});

</script>
